const formatDate = () => {
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const secs = date.getSeconds();
    return [year, month, day].map(formatNumber).join("-") + "（" + [hour, minute, secs].map(formatNumber).join(':') + "）"
};

const formatNumber = (n) => {
    n = n.toString();
    return n[1] ? n : "0" + n;
};

const hintToast = (title = '', time = '1000', icon = 'none') => {
    wx.showToast({
        title: title,
        icon: icon,
        duration: time,
        mask: true,
    });
}

const requestTimeOut = (content = '', callback) => {
    wx.showModal({
        title: "温馨提示",
        content: content,
        showCancel: false,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
        success: res => {
            callback(res)
        }
    })
}

const errorModal = (err) => {
    if (typeof err === 'string') {
        return
    }
    wx.showModal({
        title: '温馨提示',
        content: JSON.stringify(err),
        showCancel: false,
        cancelText: '取消',
        cancelColor: '#000000',
        confirmText: '确定',
        confirmColor: '#3CC51F',
    })
}

const cloudRequest = (name, data, callback) => {
    wx.showLoading({
        title: '加载中',
        mask: true,
    });
    wx.cloud.callFunction({
        name: name,
        data: data,
        success: (res) => {
            wx.hideLoading();
            callback(res);
        },
        fail: (err) => {
            errorModal(err)
            wx.hideLoading();
        }
    })
}

export default {
    formatDate,
    formatNumber,
    hintToast,
    requestTimeOut,
    errorModal,
    cloudRequest
};